/*****************************
 *@file: HeroBaseFightComp
 *@author: 陈吕唐
 *@desc: 英雄基础战斗组件
 *@date: 2024-02-27	16:08
 *****************************/
import { Collider2D, IPhysics2DContact, NodePool, _decorator, sp, v2 } from 'cc';
import { BaseFightBoxComp } from './BaseFightBoxComp';
import { BaseFightLogic } from './core/BaseFightLogic';
import IBaseFightAttribute from './core/data/IBaseFightAttribute';
import { LifeEnum } from './core/enum/LifeEnum';
import { ActionEnum } from './enum/MonsterActionEnum';
import SimpleShootAttack from './fightAction/SimpleShootAttack';
const { ccclass, property } = _decorator;

@ccclass('HeroBaseFightComp')
export class HeroBaseFightComp extends BaseFightBoxComp {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/
    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/
    /**
     * 当前行动状态
     */
    private _statue: ActionEnum = ActionEnum.INIT;

    protected downTime: number = 0;

    protected bulletPool: NodePool = new NodePool(`BaseBulletComp`);
    /***************************************************************
    *PropertyUI 引用的控件
    ***************************************************************/
    @property({ group: { name: 'Hero', id: '3' }, type: sp.Skeleton, tooltip: `英雄动画组件` })
    protected spine: sp.Skeleton = undefined!;
    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    protected update(dt: number): void {

        if (this.downTime <= 0) {
            return;
        }

        this.downTime -= dt;

        if (this.downTime <= 0) {
            this.statue = ActionEnum.IDLE;
        }
    }
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/
    public init(value: IBaseFightAttribute): void {
        super.init(value);
        this.fightAction = new SimpleShootAttack(this, this.spine);
    }


    public fightStart(): void {
        super.fightStart();
        this.statue = ActionEnum.IDLE;
    }

    protected onBeginSensorContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact): void {
        super.onBeginSensorContact(selfCollider, otherCollider, contact);
        console.log(`当前英雄范围内有怪物`)
        console.log(this.sensorFightSet)
        this.updateStatue()
    }
    /****************************************************************************************************************
     * private 私有方法
     ****************************************************************************************************************/

    /**
     * 状态改变时候调用
     */
    private updateStatue(value: ActionEnum = this.statue) {
        switch (value) {
            case ActionEnum.SHOW:
                break;
            case ActionEnum.IDLE:
                this.playAnimation('idle', true);
                this.body.linearVelocity = v2(0, 0);
                let promise = this.action();
                promise.then(
                    (value) => {
                        this.statue = value ? ActionEnum.DETAIL : ActionEnum.IDLE;
                        if (value) {
                            this.downTime = 3;
                        }
                    }, (error) => {
                        // error(error)
                        console.error(`行动错误`)
                        console.error(error)
                    }
                )
                break;
            case ActionEnum.DETAIL:
                this.playAnimation('idle', true);
                this.body.linearVelocity = v2(0, 0);
                break;
            default:
                break;
        }
    }

    protected playAnimation(name: string, loop: boolean = false): void {
        this.spine?.setAnimation(0, name, loop)
    }

    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/
    public get enemy(): BaseFightLogic[] {
        let array = Array.from(this.sensorFightSet);
        array = array.filter(v => v.lifeState != LifeEnum.DEAD)
        return array;
    }
    public get statue(): ActionEnum {
        return this._statue;
    }
    public set statue(value: ActionEnum) {
        if (this._statue === value) {
            return;
        }

        this._statue = value;
        this.updateStatue(value);
    }
}


